package com.dungpt.truyenhay.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.dungpt.truyenhay.R;
import com.dungpt.truyenhay.app.AppController;
import com.dungpt.truyenhay.model.Story;

public class CustomListStoryAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Story> storyItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListStoryAdapter(Activity activity, List<Story> storyItems) {
        this.activity = activity;
        this.storyItems = storyItems;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return storyItems.size();
    }

    @Override
    public Object getItem(int position) {
        return storyItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row_story, parent, false);
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.imgThumbnail);
        TextView name = (TextView) convertView.findViewById(R.id.tvName);
        TextView rating = (TextView) convertView.findViewById(R.id.tvRating);
        TextView chapter = (TextView) convertView.findViewById(R.id.tvChapter);
        TextView dateUpdate = (TextView) convertView.findViewById(R.id.tvDatUpdate);

        Story m = storyItems.get(position);
        thumbNail.setImageUrl(m.getImgurl(), imageLoader);
        name.setText(m.getName());
        rating.setText("Rating: "
                + String.valueOf(10 * Float.parseFloat(m.getRatingtotal())
                / Float.parseFloat(m.getRatingcount())));
        chapter.setText(m.getChapter());
        dateUpdate.setText(m.getDateupdate());

        return convertView;
    }

}
